﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class PlcWriteDbInfoRepository : BaseRepository<PlcWriteDbInfo>, IPlcWriteDbInfoRepository
    {
        public PlcWriteDbInfoRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<PageDto<PlcWriteDbInfoDto>> GetPlcWriteDbInfoAsync(Expression<Func<PlcWriteDbInfo, DispatchEquipmentInfo, PlcConnectionInfo, bool>> exp, PageModel page)
        {
            PageDto<PlcWriteDbInfoDto> pageDto = new PageDto<PlcWriteDbInfoDto>();
            RefAsync<int> totalNumber = 0;
            var result = await Context.Queryable<PlcWriteDbInfo, DispatchEquipmentInfo, PlcConnectionInfo>((a, b, c) => new JoinQueryInfos(
                JoinType.Left, a.EquipmentCode.ToString() == b.EquipmentCode,
                JoinType.Left, a.PlcConnectionId == c.ID))
                .Where(exp)
                .Select((a, b, c) => new PlcWriteDbInfoDto
                {
                    ID = a.ID,
                    EquipmentCode = a.EquipmentCode.ToString(),
                    PlcConnectionId = a.PlcConnectionId,
                    DbCode = a.DbCode,
                    DbStart = a.DbStart,
                    ObjectType = a.ObjectType,
                    PlcConnectionIP = c.IP,
                    PlcCpuType = c.PlcCpuType.ToString(),
                }).ToPageListAsync(page.PageIndex, page.PageSize, totalNumber);

            pageDto.Result = result;
            pageDto.Total = totalNumber;
            return pageDto;
        }

        public async Task<List<PlcWriteDbDto>> GetPlcWriteDbAsync()
        {
            var result = await Context.Queryable<PlcWriteDbInfo, PlcConnectionInfo>((a, b) => new JoinQueryInfos(JoinType.Inner, a.PlcConnectionId == b.ID))
                .Select((a, b) => new PlcWriteDbDto
                {
                    PlcConnectionId = a.PlcConnectionId,
                    EquipmentCode = a.EquipmentCode.ToString(),
                    PlcDbCode = 0,
                    PlcDbStart = 0,
                    WcsDbCode = a.DbCode,
                    WcsDbStart = a.DbStart,
                    IP = b.IP,
                    PlcCpuType = b.PlcCpuType.ToString(),
                    Rack = b.Rack,
                    Slot = b.Slot,
                    IsSc = b.IsSc,
                }).ToListAsync();

            var plcRedaConfigs = await Context.Queryable<PlcReadDbInfo, BytesMapObjectInfo>((a, b) => new JoinQueryInfos(JoinType.Inner, a.ID == b.PlcReadDbId))
                .Where((a, b) => b.ObjectName.Contains("Plc"))
                .Select((a, b) => new
                {
                    PlcConnectionID = a.PlcConnectionID,
                    DbCode = a.DbCode,
                    DbStart = a.DbStart
                })
                .ToListAsync();

            result.ForEach(c =>
            {
                c.PlcDbCode = plcRedaConfigs.FirstOrDefault(it => it.PlcConnectionID == c.PlcConnectionId)?.DbCode ?? 0;
                c.PlcDbStart = plcRedaConfigs.FirstOrDefault(it => it.PlcConnectionID == c.PlcConnectionId)?.DbStart ?? 0;
            });

            return result;
        }
    }
}
